package com.mqb.n4.deadlock.v2;

import com.mqb.n2.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

/**
 * @author mqb
 * @date 2021/4/24 13:28
 */
@Slf4j(topic = "c.TestDeadLock")
class TestDeadLock {
    public static void main(String[] args) {
        test1();
    }

    private static void test1(){
        final Object A = new Object();
        final Object B = new Object();
        Thread t1 = new Thread(()->{
            synchronized (A){
                log.debug("lock A");
                Sleeper.sleep(1);
                synchronized (B){
                    log.debug("lock B");
                    log.debug("操作");
                }
            }
        },"t1");

        Thread t2 = new Thread(()->{
            synchronized (B){
                log.debug("lock B");
                Sleeper.sleep(1);
                synchronized (A){
                    log.debug("lock A");
                    log.debug("操作");
                }
            }
        },"t2");

        t1.start();
        t2.start();
    }
}


